<?php

declare(strict_types=1);

namespace app\shop\controller\api\order;

use think\Request;
use think\facade\Db;
use sheep\exception\SheepException;
use app\shop\controller\api\ShopBase;
use app\shop\model\order\Express as OrderExpressModel;
use app\shop\library\express\Express as ExpressLib;

class Express extends ShopBase
{
    public function list(Request $request, $order_id)
    {
        $user = $this->auth()->user();
        // 更新包裹信息（5分钟缓存）
        (new ExpressLib)->updateOrderExpress($order_id);

        $expresses = OrderExpressModel::with(['logs', 'items' => function ($query) use ($order_id) {
            return $query->where('order_id', $order_id);
        }])->where('user_id', $user->id)->where('order_id', $order_id)->select();

        return success('获取成功', $expresses);
    }


    public function detail($id, $order_id)
    {
        $user = $this->auth()->user();
        // 更新包裹信息（5分钟缓存）
        (new ExpressLib)->updateOrderExpress($order_id);

        $express = OrderExpressModel::with(['logs', 'items' => function ($query) use ($order_id) {
            return $query->where('order_id', $order_id);
        }])->where('user_id', $user->id)->where('order_id', $order_id)->find($id);

        return success('获取成功', $express);
    }



    /**
     * 接受物流推送
     *
     * @param Request $request
     * @return void
     */
    public function push(Request $request)
    {
        $data = $request->param();
        $expressLib = new ExpressLib();
        $result = $expressLib->push($data);
        return response($result, 200, [], 'json');
    }
}
